#include "../source/server.hpp"

void onMessage(const ConnectionPtr &conn,Buffer *buffer)
{
    size_t readable = buffer->ReadAbleSize();
    if (readable > 0) {
        std::string msg = buffer->ReadStringAndPop(readable);
        DLOG("收到数据: %s", msg.c_str());
        std::string reply = "你好: " + msg;
        conn->Send(reply.c_str(), reply.size());
        //conn->ShutDown();
        //DLOG("发送成功");
    }
}
void onClose(const ConnectionPtr &conn)
{
    DLOG("CLOSE CONNECTION:%p", conn.get());
}
void onConnect(const ConnectionPtr &conn)
{
    DLOG("NEW CONNECTION:%p", conn.get());
}
int main()
{
    TcpServer server(8500);
    server.SetThreadLoopCount(2);
    server.EnableInactiveRelease(10);
    server.SetConnectCallBack(onConnect);
    server.SetCloseCallBack(onClose);
    server.SetMessageCallBack(onMessage);
    server.Start();
    return 0;
}